package com.code.app.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

public class DragView extends View {

    private Paint outerPaint;

    private Paint innerPaint;

    private PointF outerPoint;

    private PointF innnerPoint;

    private float radio = 30;

    private float maxDistance = 100;

    private PointF mP0;
    private PointF mP1;
    private PointF mP2;
    private PointF mP3;

    public DragView(Context context) {
        super(context);
    }

    public DragView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    public DragView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        outerPaint = new Paint();
        outerPaint.setColor(Color.RED);
        outerPaint.setAntiAlias(true);

        innerPaint = new Paint();
        innerPaint.setColor(Color.RED);
        innerPaint.setAntiAlias(true);


        radio = dip2px(radio);
        maxDistance = dip2px(maxDistance);

    }

    private float dip2px(float dip) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        outerPoint = new PointF();
        outerPoint.x = width / 2;
        outerPoint.y = height / 2;

        innnerPoint = new PointF();
        innnerPoint.x = width / 2;
        innnerPoint.y = height / 2;
        setMeasuredDimension(width, height);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = event.getX();
                float moveY = event.getY();
                outerPoint.x = moveX;
                outerPoint.y = moveY;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float distance = (float) Math.sqrt((innnerPoint.x - outerPoint.x) * (innnerPoint.x - outerPoint.x) + (innnerPoint.y - outerPoint.y) * (innnerPoint.y - outerPoint.y));
        if (distance <= maxDistance) {
            float radioI = radio * (1 - (float) (distance / maxDistance));

            canvas.drawCircle(innnerPoint.x, innnerPoint.y, radioI, innerPaint);

            float dx = Math.abs(innnerPoint.x - outerPoint.x);
            float dy = Math.abs(innnerPoint.y - outerPoint.y);
            float tanA = dy / dx;
            float a = (float) Math.atan(tanA);

            //P0 点
            if (mP0 == null) {
                mP0 = new PointF();
            }
            mP0.x = outerPoint.x + (int) (radio * Math.sin(a));
            mP0.y = outerPoint.y - (int) (radio * Math.cos(a));

            //P1 点
            if (mP1 == null) {
                mP1 = new PointF();
            }
            mP1.x = innnerPoint.x + (int) (radioI * Math.sin(a));
            mP1.y = innnerPoint.y - (int) (radioI * Math.cos(a));


            //P2 点
            if (mP2 == null) {
                mP2 = new PointF();
            }
            mP2.x = innnerPoint.x - (int) (radioI * Math.sin(a));
            mP2.y = innnerPoint.y + (int) (radioI * Math.cos(a));


            //P3 点
            if (mP3 == null) {
                mP3 = new PointF();
            }
            mP3.x = outerPoint.x - (int) (radio * Math.sin(a));
            mP3.y = outerPoint.y + (int) (radio * Math.cos(a));

            float centerx = (innnerPoint.x + outerPoint.x) / 2;
            float centery = (innnerPoint.y + outerPoint.y) / 2;

            Path path = new Path();
            path.moveTo(mP0.x, mP0.y);
            path.quadTo(centerx, centery, mP1.x, mP1.y);
            path.lineTo(mP2.x, mP2.y);
            path.quadTo(centerx, centery, mP3.x, mP3.y);
            path.close();
            canvas.drawPath(path, innerPaint);
        }
        canvas.drawCircle(outerPoint.x, outerPoint.y, radio, outerPaint);

    }
}
